package com.lc.lc3334;

public class Test {

}

class Solution {
    public long maxScore(int[] nums) {
        int n = nums.length;
        int[] t = new int[n];
        
        long[] preGCD = new long[n];
        long[] sufGCD = new long[n];
        long[] preLCM = new long[n];
        long[] sufLCM = new long[n];
        preGCD[0] = nums[0];
        preLCM[0] = nums[0];
        for (int i = 1; i < n; i ++) {
        	preGCD[i] = gcd(preGCD[i - 1], nums[i]);
        	preLCM[i] = lcm(preLCM[i - 1], nums[i]);
        }
        long ans = preGCD[n - 1] * preLCM[n - 1];
        sufGCD[n - 1] = nums[n - 1];
        sufLCM[n - 1] = nums[n - 1];
        for (int i = n - 2; i >= 0; i --) {
        	sufGCD[i] = gcd(sufGCD[i + 1], nums[i]);
        	sufLCM[i] = lcm(sufLCM[i + 1], nums[i]);
        }
        
        for (int i = 1; i < n - 1; i ++) {
        	ans = Math.max(ans, gcd(preGCD[i - 1], sufGCD[i + 1]) * lcm(preLCM[i - 1], sufLCM[i + 1]));
        }
        System.out.println(ans);
        if (n >= 2) {
            ans = Math.max(ans, sufGCD[1] * sufLCM[1]);
            ans = Math.max(ans, preGCD[n - 2] * preLCM[n - 2]);
        }
       
        return ans;
    }
    
    public long lcm(long a, long b) {
		return a / gcd(a, b) * b;
	}
    
    public long gcd(long a,long b) {
		return b == 0 ? a : gcd(b, a % b);
	}
}
